
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.spu_serializers import *
from meiduo_admin.paginators import MyPage

# 可选一级分类信息
class GoodsCateView(ListAPIView):
    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsCateSerializer

    def get_queryset(self):
        # TODO:获取路径参数父级分类id值
        parent_id = self.kwargs.get('pk')
        if parent_id:
            # 如果请求的是子级分类
            return self.queryset.filter(parent_id=parent_id)
        else:
            # 如果请求的是一级分类
            return self.queryset.filter(parent=None)


# 可选品牌视图
class BrandSimpleView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSimpleSerializer

# SPU视图集
class SPUView(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(name__contains=keyword)
        return self.queryset.all()